package net.zui.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import net.zui.Option;
import net.zui.comp.ZBase;
import net.zui.comp.ZBaseFrame;
import net.zui.entity.ZBorder;
import net.zui.entity.ZTextRender;
import net.zui.enume.MouseState;
import net.zui.enume.TextAlign;

public class UDraw {
	private static Graphics g;
	private static Image image;
	
	static {
		image=new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);
		g=image.getGraphics();
	}
	
	/**
	 * 获取字体
	 * @param name 字体名称,为逗号分隔的字体
	 * @param size 尺寸
	 * @param bold 是否为粗体
	 * @param italic 是否为斜体
	 * @return
	 */
	public static Font getFont(String name,int size,boolean bold,boolean italic) {
		return new Font(name, (bold?Font.BOLD:Font.PLAIN)+(italic?Font.ITALIC:Font.PLAIN), size);
	}
	
	public static Point centerString(String str,Font f,int width,int height) {
		return alignString(str, f, width, height, 0, TextAlign.center);
	}
	
	public static Point center(ZBaseFrame frame,int width,int height) {
		return new Point((frame.getWidth()-width)/2,(frame.getHeight()-height)/2);
	}
	
	public static void center(ZBase base) {
		base.setX((base.getGroup().getManager().getWidth()-base.getWidth())/2);
		base.setY((base.getGroup().getManager().getHeight()-base.getHeight()/2));
	}
	
	/**
	 * 获取颜色的反色
	 * @param c 颜色
	 * @return 反色
	 */
	public static Color antiColor(Color c) {
		return new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue());
	}
	
	public static Color clone(Color c) {
		return new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
	}
	
	public static Color web(String webColor) {
		int len=webColor.length();
		if(len!=3 && len!=6) {
			UComm.info(UDraw.class, "[web]颜色字符串长度不为3或6, 返回默认的白色!");
			return Color.WHITE;
		}
		int[] rgb=new int[3];
		String tmp;
		if(len==3) {
			char[] chars=new char[6];
			for(int i=0;i<6;i++) {
				chars[i]=webColor.charAt(i/2);
			}
			tmp=new String(chars);
		}else{
			tmp=webColor;
		}
		for(int i=0;i<3;i++) {
			rgb[i]=Integer.valueOf(tmp.substring(i*2, i*2+2), 16);
		}
		return new Color(rgb[0],rgb[1],rgb[2]);
	}
	
	/**
	 * 将图片像素全部设置为透明
	 * @param image
	 * @return false- 图片为空/不支持透明
	 */
	public static boolean Alpha(BufferedImage image) {
		if(image==null)return false;
		int type = image.getType();
		boolean has=UComm.isInArray(type, BufferedImage.TYPE_INT_RGB,BufferedImage.TYPE_INT_BGR,BufferedImage.TYPE_3BYTE_BGR,BufferedImage.TYPE_BYTE_BINARY,
				BufferedImage.TYPE_BYTE_INDEXED,BufferedImage.TYPE_USHORT_GRAY,BufferedImage.TYPE_USHORT_555_RGB,BufferedImage.TYPE_USHORT_565_RGB);
		if(has)return false;
		int color = Option.baseBackColor.getRGB();
		int w=image.getWidth(),h=image.getHeight();
		for(int i=0;i<w;i++) {
			for(int j=0; j<h;j++) {
				image.setRGB(i, j, color);
			}
		}
		return true;
	}
	
	public static Point alignString(ZTextRender render,int width,int height,int padding) {
		return alignString(render.getText(),render.getFont(),width,height,padding,render.getAlign());
	}
	
	public static Point alignString(String str,Font f,int width,int height,int padding,TextAlign align) {
		FontMetrics metrics = g.getFontMetrics(f);
		int textHeight=fontHeight(f);
		int heightCenter=(height+textHeight/2)/2;
		if(align==TextAlign.left) {
			return new Point(padding,heightCenter);
		}
		int len = metrics.stringWidth(str);
		if(f.getStyle()!=0)len++;
		
		if(align==TextAlign.center) {
			return new Point((width-len)/2,heightCenter);
		}
		if(align==TextAlign.right) {
			return new Point(width-len-padding,heightCenter);
		}
		return null;
	}
	
	/**
	 * 获取字体高度
	 * @param f 字体
	 * @return 字体高度
	 */
	public static int fontHeight(Font f) {
		FontMetrics metrics = g.getFontMetrics(f);
		return metrics.getAscent()+metrics.getDescent()+metrics.getLeading();
	}
	
	public static void drawCenterStrings(Graphics2D context,List<String> list,int x,int y,int width,int height, int padding) {
		if(UComm.isNull(list))return;
		int fh=fontHeight(context.getFont());
		//M.l().debug(UDraw.class, " height:",height," allHeight:",allHeight," cy:",cy," font:",font," fh:",fh);
		for(int i=0;i<list.size();i++) {
			g.drawString(list.get(i), x, y+i*fh+(i==0?0:i-1)*padding);
		}
	}
	
	/**
	  * 绘制文字列表,如果字体样式有粗体,则绘制粗体文字,在调用本方法之前,需要给GraphicsContext先传入字体
	 * @param g 绘制工具
	 * @param list 文字列表
	 * @param x x坐标
	 * @param y y坐标,本引擎内,文字统一为顶点定位,即以左上角坐标定位
	 * @param padding 行间距
	 */
	public static void drawStrings(Graphics2D g,List<String> list,int x,int y,int padding) {
		if(UComm.isNull(list))return;
		int fh=fontHeight(g.getFont());
		for(int i=0;i<list.size();i++) {
			g.drawString(list.get(i), x, y+i*fh+(i==0?0:i-1)*padding);
		}
	}
	
	public static void drawRect(ZBorder border,MouseState state,Color backColor,ZBase base,Graphics2D g2d) {
		boolean isOne=border==null || border.getLine()==0;
		int outerWidth=base.getOuterWidth(),outerHeight=base.getOuterHeight();
		if(isOne) {		
			g2d.setColor(backColor);
			g2d.fillRect(0, 0, outerWidth-1, outerHeight-1);
			return;
		}
		int radius=border.getRadius();
		int line=border.getLine();
		g2d.setColor(state==MouseState.mouseOut?border.getBorderColor():border.getMouseInColor());
		if(radius==0) {
			g2d.fillRect(0, 0, outerWidth-1, outerHeight-1);
		}else {
			g2d.fillRoundRect(0, 0, outerWidth-1, outerHeight-1,radius,radius);
		}
		g2d.setColor(backColor);
		if(radius==0) {
			g2d.fillRect(line, line, outerWidth-line-1, outerHeight-line-1);
		}else {
			g2d.fillRoundRect(line, line, outerWidth-line-line-1, outerHeight-line-line-1,radius,radius);
		}
	}
	
	public static int stringWidth(String str,Font f) {
		return g.getFontMetrics(f).stringWidth(str);
	}
	
	public static List<String> splitString(String str,int width,Font f){
		if(UComm.isNull(str))return null;
		List<String> list=new ArrayList<String>();
		int start=0;
		double half=f.getSize()/2,one=f.getSize(),len=0.0;
		char c;
		for(int i=0;i<str.length();i++) {
			c=str.charAt(i);
			if(c=='\n') {
				list.add(str.substring(start, i+1));
				start=i+1;
				len=0;
			}
			len+=c<256?half:one;
			if(len>=width || i==str.length()-1) {
				list.add(str.substring(start, i+1));
				start=i+1;
				len=0;
			}
		}
		return list;
	}
}
